home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / CPP / WFC010.ZIP / SRC / CTAPE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-07  |  15.8 KB  |  629 lines

  1. #include <wfc.h>
  2. #pragma hdrstop
  3.  
  4. /*
  5. ** Author: Samuel R. Blackburn
  6. ** CI$: 76300,326
  7. ** Internet: sammy@sed.csc.com
  8. **
  9. ** You can use it any way you like as long as you don't try to sell it.
  10. **
  11. ** Any attempt to sell WFC in source code form must have the permission
  12. ** of the original author. You can produce commercial executables with
  13. ** WFC but you can't sell WFC.
  14. **
  15. ** Copyright, 1995, Samuel R. Blackburn
  16. **
  17. ** $Workfile: $
  18. ** $Revision: $
  19. ** $Modtime: $
  20. */
  21.  
  22. #if defined( _DEBUG )
  23. #undef THIS_FILE
  24. static char BASED_CODE THIS_FILE[] = __FILE__;
  25. #define new DEBUG_NEW
  26. #endif
  27.  
  28. /*
  29. ** CTapeGetDriveParameters
  30. */
  31.  
  32. CTapeGetDriveParameters::CTapeGetDriveParameters()
  33. {
  34.    Empty();
  35. }
  36.  
  37. CTapeGetDriveParameters::CTapeGetDriveParameters( const CTapeGetDriveParameters& source )
  38. {
  39.    Empty();
  40.    Copy( source );
  41. }
  42.  
  43. CTapeGetDriveParameters::CTapeGetDriveParameters( const TAPE_GET_DRIVE_PARAMETERS * source )
  44. {
  45.    Empty();
  46.    Copy( source );
  47. }
  48.  
  49. CTapeGetDriveParameters::~CTapeGetDriveParameters()
  50. {
  51.    Empty();
  52. }
  53.  
  54. void CTapeGetDriveParameters::Copy( const CTapeGetDriveParameters& source )
  55. {
  56.    Copy( (const TAPE_GET_DRIVE_PARAMETERS *) &source );
  57. }
  58.  
  59. void CTapeGetDriveParameters::Copy( const TAPE_GET_DRIVE_PARAMETERS * source )
  60. {
  61.    ASSERT( source != NULL );
  62.  
  63.    if ( source == NULL )
  64.    {
  65.       return;
  66.    }
  67.  
  68.    ECC                   = source->ECC;
  69.    Compression           = source->Compression;
  70.    DataPadding           = source->DataPadding;
  71.    ReportSetmarks        = source->ReportSetmarks;
  72.    DefaultBlockSize      = source->DefaultBlockSize;
  73.    MaximumBlockSize      = source->MaximumBlockSize;
  74.    MinimumBlockSize      = source->MinimumBlockSize;
  75.    MaximumPartitionCount = source->MaximumPartitionCount;
  76.    FeaturesLow           = source->FeaturesLow;
  77.    FeaturesHigh          = source->FeaturesHigh;
  78.    EOTWarningZoneSize    = source->EOTWarningZoneSize;
  79. }
  80.  
  81. #if defined( _DEBUG )
  82.  
  83. void CTapeGetDriveParameters::Dump( CDumpContext& dump_context ) const
  84. {
  85.    dump_context << " a CTapeGetDriveParameters at " << (void *) this << "\n";
  86.    dump_context << "{\n";
  87.    dump_context << "   ECC = "                   << ECC                   << "\n";
  88.    dump_context << "   Compression = "           << Compression           << "\n";
  89.    dump_context << "   DataPadding = "           << DataPadding           << "\n";
  90.    dump_context << "   ReportSetmarks = "        << ReportSetmarks        << "\n";
  91.    dump_context << "   DefaultBlockSize = "      << DefaultBlockSize      << "\n";
  92.    dump_context << "   MaximumBlockSize = "      << MaximumBlockSize      << "\n";
  93.    dump_context << "   MinimumBlockSize = "      << MinimumBlockSize      << "\n";
  94.    dump_context << "   MaximumPartitionCount = " << MaximumPartitionCount << "\n";
  95.    dump_context << "   FeaturesLow = "           << FeaturesLow           << "\n";
  96.    dump_context << "   FeaturesHigh = "          << FeaturesHigh          << "\n";
  97.    dump_context << "   EOTWarningZoneSize = "    << EOTWarningZoneSize    << "\n";
  98.    dump_context << "}\n";
  99. }
  100.  
  101. #endif // _DEBUG
  102.  
  103. void CTapeGetDriveParameters::Empty( void )
  104. {
  105.    ECC                   = 0;
  106.    Compression           = 0;
  107.    DataPadding           = 0;
  108.    ReportSetmarks        = 0;
  109.    DefaultBlockSize      = 0;
  110.    MaximumBlockSize      = 0;
  111.    MinimumBlockSize      = 0;
  112.    MaximumPartitionCount = 0;
  113.    FeaturesLow           = 0;
  114.    FeaturesHigh          = 0;
  115.    EOTWarningZoneSize    = 0;
  116. }
  117.  
  118. /*
  119. ** CTapeGetMediaParameters
  120. */
  121.  
  122. CTapeGetMediaParameters::CTapeGetMediaParameters()
  123. {
  124.    Empty();
  125. }
  126.  
  127. CTapeGetMediaParameters::CTapeGetMediaParameters( const CTapeGetMediaParameters& source )
  128. {
  129.    Empty();
  130.    Copy( source );
  131. }
  132.  
  133. CTapeGetMediaParameters::CTapeGetMediaParameters( const TAPE_GET_MEDIA_PARAMETERS * source )
  134. {
  135.    Empty();
  136.    Copy( source );
  137. }
  138.  
  139. CTapeGetMediaParameters::~CTapeGetMediaParameters()
  140. {
  141.    Empty();
  142. }
  143.  
  144. void CTapeGetMediaParameters::Copy( const CTapeGetMediaParameters& source )
  145. {
  146.    Copy( (const TAPE_GET_MEDIA_PARAMETERS *) &source );
  147. }
  148.  
  149. void CTapeGetMediaParameters::Copy( const TAPE_GET_MEDIA_PARAMETERS *source )
  150. {
  151.    ASSERT( source != NULL );
  152.  
  153.    if ( source == NULL )
  154.    {
  155.       return;
  156.    }
  157.  
  158.    Capacity.LowPart   = source->Capacity.LowPart;
  159.    Capacity.HighPart  = source->Capacity.HighPart;
  160.    Remaining.LowPart  = source->Remaining.LowPart;
  161.    Remaining.HighPart = source->Remaining.HighPart;
  162.    BlockSize          = source->BlockSize;
  163.    PartitionCount     = source->PartitionCount;
  164.    WriteProtected     = source->WriteProtected;
  165. }
  166.  
  167. #if defined( _DEBUG )
  168.  
  169. void CTapeGetMediaParameters::Dump( CDumpContext& dump_context ) const
  170. {
  171.    dump_context << " a CTapeGetMediaParameters at " << (void *) this << "\n";
  172.    dump_context << "{\n";
  173.    dump_context << "   Capacity.LowPart = "  << Capacity.LowPart   << "\n";
  174.    dump_context << "   Capacity.HighPart = " << Capacity.HighPart  << "\n";
  175.    dump_context << "   Remaining.LowPart = " << Remaining.LowPart  << "\n";
  176.    dump_context << "   Remaining.HighPart = "<< Remaining.HighPart << "\n";
  177.    dump_context << "   BlockSize = "         << BlockSize          << "\n";
  178.    dump_context << "   PartitionCount = "    << PartitionCount     << "\n";
  179.    dump_context << "   WriteProtected = "    << WriteProtected     << "\n";
  180.    dump_context << "}\n";
  181. }
  182.  
  183. #endif // _DEBUG
  184.  
  185. void CTapeGetMediaParameters::Empty( void )
  186. {
  187.    Capacity.LowPart   = 0;
  188.    Capacity.HighPart  = 0;
  189.    Remaining.LowPart  = 0;
  190.    Remaining.HighPart = 0;
  191.    BlockSize          = 0;
  192.    PartitionCount     = 0;
  193.    WriteProtected     = 0;
  194. }
  195.  
  196. /*
  197. ** CTapeSetDriveParameters
  198. */
  199.  
  200. CTapeSetDriveParameters::CTapeSetDriveParameters()
  201. {
  202.    Empty();
  203. }
  204.  
  205. CTapeSetDriveParameters::CTapeSetDriveParameters( const CTapeSetDriveParameters& source )
  206. {
  207.    Empty();
  208.    Copy( source );
  209. }
  210.  
  211. CTapeSetDriveParameters::CTapeSetDriveParameters( const TAPE_SET_DRIVE_PARAMETERS * source )
  212. {
  213.    Empty();
  214.    Copy( source );
  215. }
  216.  
  217. CTapeSetDriveParameters::~CTapeSetDriveParameters()
  218. {
  219.    Empty();
  220. }
  221.  
  222. void CTapeSetDriveParameters::Copy( const CTapeSetDriveParameters& source )
  223. {
  224.    Copy( (const TAPE_SET_DRIVE_PARAMETERS * ) &source );
  225. }
  226.  
  227. void CTapeSetDriveParameters::Copy( const TAPE_SET_DRIVE_PARAMETERS * source )
  228. {
  229.    ASSERT( source != NULL );
  230.  
  231.    if ( source == NULL )
  232.    {
  233.       return;
  234.    }
  235.  
  236.    ECC                = source->ECC;
  237.    Compression        = source->Compression;
  238.    ReportSetmarks     = source->ReportSetmarks;
  239.    EOTWarningZoneSize = source->EOTWarningZoneSize;
  240. }
  241.  
  242. #if defined( _DEBUG )
  243.  
  244. void CTapeSetDriveParameters::Dump( CDumpContext& dump_context ) const
  245. {
  246.    dump_context << " a CTapeSetDriveParameters at " << (void *) this << "\n";
  247.    dump_context << "{\n";
  248.    dump_context << "   ECC = "               << ECC                << "\n";
  249.    dump_context << "   Compression = "       << Compression        << "\n";
  250.    dump_context << "   ReportSetmarks = "    << ReportSetmarks     << "\n";
  251.    dump_context << "   EOTWarningZoneSize = "<< EOTWarningZoneSize << "\n";
  252.    dump_context << "}\n";
  253. }
  254.  
  255. #endif // _DEBUG
  256.  
  257. void CTapeSetDriveParameters::Empty( void )
  258. {
  259.    ECC                = 0;
  260.    Compression        = 0;
  261.    DataPadding        = 0;
  262.    ReportSetmarks     = 0;
  263.    EOTWarningZoneSize = 0;
  264. }
  265.  
  266. /*
  267. ** CTapeSetMediaParameters
  268. */
  269.  
  270. CTapeSetMediaParameters::CTapeSetMediaParameters()
  271. {
  272.    Empty();
  273. }
  274.  
  275. CTapeSetMediaParameters::CTapeSetMediaParameters( const CTapeSetMediaParameters& source )
  276. {
  277.    Empty();
  278.    Copy( source );
  279. }
  280.  
  281. CTapeSetMediaParameters::CTapeSetMediaParameters( const TAPE_SET_MEDIA_PARAMETERS * source )
  282. {
  283.    Empty();
  284.    Copy( source );
  285. }
  286.  
  287. CTapeSetMediaParameters::~CTapeSetMediaParameters()
  288. {
  289.    Empty();
  290. }
  291.  
  292. void CTapeSetMediaParameters::Copy( const CTapeSetMediaParameters& source )
  293. {
  294.    Copy( (const TAPE_SET_MEDIA_PARAMETERS *) &source );
  295. }
  296.  
  297. void CTapeSetMediaParameters::Copy( const TAPE_SET_MEDIA_PARAMETERS * source )
  298. {
  299.    ASSERT( source != NULL );
  300.  
  301.    if ( source == NULL )
  302.    {
  303.       return;
  304.    }
  305.  
  306.    BlockSize = source->BlockSize;
  307. }
  308.  
  309. #if defined( _DEBUG )
  310.  
  311. void CTapeSetMediaParameters::Dump( CDumpContext& dump_context ) const
  312. {
  313.    dump_context << " a CTapeSetMediaParameters at " << (void *) this << "\n";
  314.    dump_context << "{\n";
  315.    dump_context << "   BlockSize = " << BlockSize << "\n";
  316.    dump_context << "}\n";
  317. }
  318.  
  319. #endif // _DEBUG
  320.  
  321. void CTapeSetMediaParameters::Empty( void )
  322. {
  323.    BlockSize = 0;
  324. }
  325.  
  326. /*
  327. ** CTape
  328. */
  329.  
  330. CTape::CTape()
  331. {
  332.    m_Initialize();
  333.    m_AutomaticallyClose = FALSE;
  334. }
  335.  
  336. CTape::~CTape()
  337. {
  338.    if ( m_AutomaticallyClose != FALSE )
  339.    {
  340.       Close();
  341.    }
  342.  
  343.    m_Initialize();
  344. }
  345.  
  346. void CTape::m_Initialize( void )
  347. {
  348.    m_BackupReadContextPointer  = NULL;
  349.    m_BackupWriteContextPointer = NULL;
  350. }
  351.  
  352. void CTape::Close( void )
  353. {
  354.    if ( m_hFile != NULL )
  355.    {
  356.       ::CloseHandle( (HANDLE) m_hFile );
  357.    }
  358. }
  359.  
  360. BOOL CTape::CreatePartition( const TypeOfPartition type_of_partition, 
  361.                              const DWORD           number_of_partitions, 
  362.                              const DWORD           number_of_megabytes_in_each_partition )
  363. {
  364.    m_ErrorCode = ::CreateTapePartition( (HANDLE) m_hFile, (DWORD) type_of_partition, number_of_partitions, number_of_megabytes_in_each_partition );
  365.  
  366.    if ( m_ErrorCode == NO_ERROR )
  367.    {
  368.       return( TRUE );
  369.    }
  370.    else
  371.    {
  372.       return( FALSE );
  373.    }
  374. }
  375.  
  376. BOOL CTape::Erase( const TypeOfErasure type_of_erasure, const BOOL return_immediately )
  377. {
  378.    m_ErrorCode = ::EraseTape( (HANDLE) m_hFile, type_of_erasure, return_immediately );
  379.  
  380.    if ( m_ErrorCode == NO_ERROR )
  381.    {
  382.       return( TRUE );
  383.    }
  384.    else
  385.    {
  386.       return( FALSE );
  387.    }
  388. }
  389.  
  390. BOOL CTape::GetParameters( CTapeGetDriveParameters& parameters )
  391. {
  392.    DWORD size = sizeof( TAPE_GET_DRIVE_PARAMETERS );
  393.  
  394.    TAPE_GET_DRIVE_PARAMETERS drive_parameters;
  395.  
  396.    ::ZeroMemory( &drive_parameters, size );
  397.  
  398.    m_ErrorCode = ::GetTapeParameters( (HANDLE) m_hFile, GET_TAPE_DRIVE_INFORMATION, &size, &drive_parameters );
  399.  
  400.    if ( m_ErrorCode == NO_ERROR )
  401.    {
  402.       parameters.Copy( &drive_parameters );
  403.       return( TRUE );
  404.    }
  405.    else
  406.    {
  407.       return( FALSE );
  408.    }
  409. }
  410.  
  411. BOOL CTape::GetParameters( CTapeGetMediaParameters& parameters )
  412. {
  413.    DWORD size = sizeof( TAPE_GET_MEDIA_PARAMETERS );
  414.  
  415.    TAPE_GET_MEDIA_PARAMETERS media_parameters;
  416.  
  417.    ::ZeroMemory( &media_parameters, size );
  418.  
  419.    m_ErrorCode = ::GetTapeParameters( (HANDLE) m_hFile, GET_TAPE_MEDIA_INFORMATION, &size, &media_parameters );
  420.  
  421.    if ( m_ErrorCode == NO_ERROR )
  422.    {
  423.       parameters.Copy( &media_parameters );
  424.       return( TRUE );
  425.    }
  426.    else
  427.    {
  428.       return( FALSE );
  429.    }
  430. }
  431.  
  432. BOOL CTape::GetPosition( const Position type_of_position_to_get, DWORD& partition_number, DWORD& position_low, DWORD& position_high )
  433. {
  434.    m_ErrorCode = ::GetTapePosition( (HANDLE) m_hFile, type_of_position_to_get, &partition_number, &position_low, &position_high );
  435.  
  436.    if ( m_ErrorCode == NO_ERROR )
  437.    {
  438.       return( TRUE );
  439.    }
  440.    else
  441.    {
  442.       return( FALSE );
  443.    }
  444. }
  445.  
  446. DWORD CTape::GetStatus( void ) const
  447. {
  448.    return( ::GetTapeStatus( (HANDLE) m_hFile ) );
  449. }
  450.  
  451. BOOL CTape::Mark( const TypeOfMark type_of_mark, const DWORD number_of_marks, const BOOL return_immediately )
  452. {
  453.    m_ErrorCode = ::WriteTapemark( (HANDLE) m_hFile, type_of_mark, number_of_marks, return_immediately );
  454.  
  455.    if ( m_ErrorCode == NO_ERROR )
  456.    {
  457.       return( TRUE );
  458.    }
  459.    else
  460.    {
  461.       return( FALSE );
  462.    }
  463. }
  464.  
  465. BOOL CTape::Load( void )
  466. {
  467.    return( Prepare( LoadTape ) );
  468. }
  469.  
  470. BOOL CTape::Lock( void )
  471. {
  472.    return( Prepare( LockTape ) );
  473. }
  474.  
  475. BOOL CTape::Open( const UINT tape_drive_number_starting_at_zero )
  476. {
  477.    TCHAR tape_drive_name[ MAX_PATH ];
  478.  
  479.    ::ZeroMemory( tape_drive_name, sizeof( tape_drive_name ) );
  480.  
  481.    ::wsprintf( tape_drive_name, "\\\\.\\TAPE%u", tape_drive_number_starting_at_zero );
  482.  
  483.    HANDLE file_handle = INVALID_HANDLE_VALUE;
  484.  
  485.    file_handle = ::CreateFile( tape_drive_name,
  486.                                GENERIC_READ | GENERIC_WRITE,
  487.                                0,
  488.                                0,
  489.                                OPEN_EXISTING,
  490.                                0,
  491.                                NULL );
  492.  
  493.    if ( file_handle == INVALID_HANDLE_VALUE )
  494.    {
  495.       m_ErrorCode = ::GetLastError();
  496.       return( FALSE );
  497.    }
  498.  
  499.    m_hFile = (UINT) file_handle;
  500.  
  501.    return( TRUE );
  502. }
  503.  
  504. BOOL CTape::Prepare( const Operation what_to_do, const BOOL return_immediately )
  505. {
  506.    m_ErrorCode = ::PrepareTape( (HANDLE) m_hFile, what_to_do, return_immediately );
  507.  
  508.    if ( m_ErrorCode == NO_ERROR )
  509.    {
  510.       return( TRUE );
  511.    }
  512.    else
  513.    {
  514.       return( FALSE );
  515.    }
  516. }
  517.  
  518. BOOL CTape::Read( LPBYTE buffer, const DWORD number_of_bytes_to_read, LPDWORD number_of_bytes_read, BOOL abort, BOOL restore_security_data )
  519. {
  520.    BOOL return_value = ::BackupRead( (HANDLE) m_hFile,
  521.                                      buffer,
  522.                                      number_of_bytes_to_read,
  523.                                      number_of_bytes_read,
  524.                                      abort,
  525.                                      restore_security_data,
  526.                                      &m_BackupReadContextPointer );
  527.  
  528.    if ( return_value != TRUE )
  529.    {
  530.       m_ErrorCode = ::GetLastError();
  531.    }
  532.  
  533.    return( return_value );
  534. }
  535.  
  536. BOOL CTape::Seek( const DWORD seek_low, const DWORD seek_high, LPDWORD seeked_low, LPDWORD seeked_high )
  537. {
  538.    BOOL return_value = ::BackupSeek( (HANDLE) m_hFile, seek_low, seek_high, seeked_low, seeked_high, &m_BackupReadContextPointer );
  539.  
  540.    if ( return_value != TRUE )
  541.    {
  542.       m_ErrorCode = ::GetLastError();
  543.    }
  544.  
  545.    return( return_value );
  546. }
  547.  
  548. BOOL CTape::SetAutomaticallyClose( BOOL auto_close )
  549. {
  550.    BOOL return_value = m_AutomaticallyClose;
  551.  
  552.    m_AutomaticallyClose = auto_close;
  553.  
  554.    return( return_value );
  555. }
  556.  
  557. BOOL CTape::SetParameters( const CTapeSetMediaParameters& parameters )
  558. {
  559.    CTapeSetMediaParameters media_parameters( parameters );
  560.  
  561.    m_ErrorCode = ::SetTapeParameters( (HANDLE) m_hFile, SET_TAPE_MEDIA_INFORMATION, (TAPE_SET_MEDIA_PARAMETERS *) &media_parameters );
  562.  
  563.    if ( m_ErrorCode == NO_ERROR )
  564.    {
  565.       return( TRUE );
  566.    }
  567.    else
  568.    {
  569.       return( FALSE );
  570.    }
  571. }
  572.  
  573. BOOL CTape::SetParameters( const CTapeSetDriveParameters& parameters )
  574. {
  575.    CTapeSetDriveParameters drive_parameters( parameters );
  576.  
  577.    m_ErrorCode = ::SetTapeParameters( (HANDLE) m_hFile, SET_TAPE_DRIVE_INFORMATION, (TAPE_SET_DRIVE_PARAMETERS *) &drive_parameters );
  578.  
  579.    if ( m_ErrorCode == NO_ERROR )
  580.    {
  581.       return( TRUE );
  582.    }
  583.    else
  584.    {
  585.       return( FALSE );
  586.    }
  587. }
  588.  
  589. BOOL CTape::SetPosition( const PositionMethod how_to_get_there, const DWORD partition_number, const DWORD position_low, const DWORD position_high, const BOOL return_immediately )
  590. {
  591.    m_ErrorCode = ::SetTapePosition( (HANDLE) m_hFile, how_to_get_there, partition_number, position_low, position_high, return_immediately );
  592.  
  593.    if ( m_ErrorCode == NO_ERROR )
  594.    {
  595.       return( TRUE );
  596.    }
  597.    else
  598.    {
  599.       return( FALSE );
  600.    }
  601. }
  602.  
  603. BOOL CTape::Unload( void )
  604. {
  605.    return( Prepare( UnloadTape ) );
  606. }
  607.  
  608. BOOL CTape::Unlock( void )
  609. {
  610.    return( Prepare( UnlockTape ) );
  611. }
  612.  
  613. BOOL CTape::Write( LPBYTE buffer, const DWORD number_of_bytes_to_write, LPDWORD number_of_bytes_written, BOOL abort, BOOL restore_security_data )
  614. {
  615.    BOOL return_value = ::BackupWrite( (HANDLE) m_hFile,
  616.                                       buffer,
  617.                                       number_of_bytes_to_write,
  618.                                       number_of_bytes_written,
  619.                                       abort,
  620.                                       restore_security_data,
  621.                                       &m_BackupWriteContextPointer );
  622.    if ( return_value != TRUE )
  623.    {
  624.       m_ErrorCode = ::GetLastError();
  625.    }
  626.  
  627.    return( return_value );
  628. }
  629.